package o;

import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.appgallery.downloadengine.api.DownloadChkInfo;
import com.huawei.appgallery.downloadengine.api.SplitTask;
import com.huawei.appgallery.foundation.store.bean.spilt.GetApksInfoResponse.Slice;
import com.huawei.appgallery.foundation.store.bean.spilt.GetApksInfoResponse.SplitApkInfo;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class bpf implements bkm {
    private static final String TAG = "SplitTaskBuilder";
    private String mFileSha256;
    private long mFileSize;
    private int mFileType;
    private String mSlices;
    private String mTarget;
    private String mUrl;
    private String mValue;

    public bpf ˏ(String str, String str2) {
        this.mTarget = str;
        this.mValue = str2;
        return this;
    }

    public bpf ˊ(SplitApkInfo splitApkInfo) {
        if (splitApkInfo != null) {
            this.mFileType = splitApkInfo.ˎ();
            this.mUrl = splitApkInfo.ˋ();
            this.mFileSize = splitApkInfo.ˊ();
            this.mFileSha256 = splitApkInfo.ˏ();
            this.mSlices = ॱ(splitApkInfo.ॱ()).toString();
        }
        return this;
    }

    @NonNull
    private JSONArray ॱ(@Nullable List<Slice> list) {
        JSONArray jSONArray = new JSONArray();
        if (list != null) {
            try {
                for (Slice toJson : list) {
                    jSONArray.put(new JSONObject(toJson.toJson()));
                }
            } catch (JSONException e) {
                boc.ˎ.ˊ(TAG, "Exception when creating JSONArray for sliceList: " + e.getMessage());
            } catch (IllegalAccessException e2) {
                boc.ˎ.ˊ(TAG, "Exception when creating JSONArray for sliceList: " + e2.getMessage());
            }
        }
        return jSONArray;
    }

    @NonNull
    public SplitTask ˎ() {
        SplitTask splitTask = new SplitTask();
        splitTask.ॱॱ(this.mTarget);
        splitTask.ᐝ(this.mValue);
        splitTask.ˎ(this.mFileType);
        splitTask.ˋ(this.mUrl);
        splitTask.ˋ(this.mFileSize);
        splitTask.ˊ(this.mFileSha256);
        List ॱ = ॱ(this.mSlices);
        if (!ॱ.isEmpty()) {
            splitTask.ˋ(ॱ);
        }
        return splitTask;
    }

    @NonNull
    private static List<DownloadChkInfo> ॱ(@Nullable String str) {
        List arrayList = new ArrayList();
        if (!TextUtils.isEmpty(str)) {
            try {
                JSONArray jSONArray = new JSONArray(str);
                for (int i = 0; i < jSONArray.length(); i++) {
                    Slice slice = new Slice();
                    slice.fromJson(jSONArray.getJSONObject(i));
                    DownloadChkInfo downloadChkInfo = new DownloadChkInfo();
                    downloadChkInfo.ˏ(slice.ˋ());
                    long[] ˎ = ˎ(bkz.ˊ(slice.ॱ()));
                    downloadChkInfo.ˏ(ˎ[0]);
                    downloadChkInfo.ˋ(ˎ[1]);
                    downloadChkInfo.ॱ(0);
                    arrayList.add(downloadChkInfo);
                }
            } catch (Exception e) {
                boc.ˎ.ˊ(TAG, "Exception when parsing JSONArray for sliceList: " + e.getMessage());
            }
        }
        return arrayList;
    }

    @NonNull
    private static long[] ˎ(@NonNull final String str) throws NumberFormatException {
        if (str.split("-").length == 2) {
            return new long[]{Long.parseLong(str.split("-")[0]), Long.parseLong(str.split("-")[1])};
        }
        throw new NumberFormatException() {
            public String getMessage() {
                return "Failed to parse slice index: " + str;
            }
        };
    }
}
